var url = require('url');
function router(obj){
  return function(req,res,next){
    if(!(obj[req.method]))
      next();
      return;
  }
  var routes = obj[req.method];
  var url = url.parse(req.url); 
  var paths = Object.keys(routes);
  for (var i=0 ; i<paths.length; i++){
    var path = paths[i];
    var fn = routes[path];
    var path = path.replace(/\//g, '\\/').replace(/:(\w+)/g,'([^\\/]+)');
    var re = new RegExp('^'+ path +'$');
    console.log(url.pathname);
    var captures = url.pathname.match(re);
    console.log(captures);
    if (captures){
      var args = [req,res].concat(captures.slice(1));
      fn.apply(null, args);
      return;
    }
  }
  next();
};
module.exports = router;
